package com.yonyou.sto.modules.ims.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.sto.common.api.CommonPage;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.modules.ims.dto.InvoiceDto;
import com.yonyou.sto.modules.ims.dto.ManualDto;
import com.yonyou.sto.modules.ims.model.ImsInvoiceItem;
import com.yonyou.sto.modules.ims.model.ImsManualInvoice;
import com.yonyou.sto.modules.ims.service.ImsInvoiceItemService;
import com.yonyou.sto.modules.ims.service.ImsManualInvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aegon
 * @since 2020-10-20
 */
@RestController
@RequestMapping("/server/ims/manual")
@Api(tags = "ImsManualInvoiceController", description = "手工开票管理")
public class ImsManualInvoiceController {

    @Resource
    private ImsManualInvoiceService manualInvoiceService;

    @Resource
    private ImsInvoiceItemService itemService;

    @ApiOperation("新增手动开票")
    @PostMapping(value = "/add")
    public CommonResult add(Principal principal, @RequestBody ManualDto dto) {
        if (principal == null) return CommonResult.unauthorized(null);
        return manualInvoiceService.add(principal, dto);
    }

    @ApiOperation("手动开票列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<ImsManualInvoice>> list(Principal principal,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "billSn", required = false) String billSn,
                                                           @RequestParam(value = "partyName", required = false) String partyName,
                                                           @RequestParam(value = "buyerName", required = false) String buyerName,
                                                           @RequestParam(value = "status", required = false) Integer status) {
        Page<ImsManualInvoice> page = manualInvoiceService.list(principal.getName(), pageSize, pageNum, billSn, partyName, buyerName, status);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("手动开票审核列表")
    @GetMapping(value = "/verifyList")
    public CommonResult<CommonPage<ImsManualInvoice>> verifyList(Principal principal,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                 @RequestParam(value = "billSn", required = false) String billSn,
                                                                 @RequestParam(value = "partyName", required = false) String partyName,
                                                                 @RequestParam(value = "buyerName", required = false) String buyerName,
                                                                 @RequestParam(value = "status", required = false) Integer status) {
        Page<ImsManualInvoice> page = manualInvoiceService.verifyList(principal.getName(), pageSize, pageNum, billSn, partyName, buyerName, status);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("详情")
    @GetMapping(value = "/info")
    public CommonResult<ManualDto> info(@RequestParam(value = "manualId") String manualId) {
        return manualInvoiceService.info(manualId);
    }

    @ApiOperation("编辑")
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody ManualDto dto) {
        return manualInvoiceService.edit(dto);
    }

    @ApiOperation("审核")
    @PostMapping(value = "/verify")
    public CommonResult verify(@RequestBody ManualDto dto) {
        return manualInvoiceService.verify(dto);
    }

    @ApiOperation("编辑时新增一条明细")
    @PostMapping("/addItem")
    public CommonResult addItem(@RequestParam String manualId, @RequestParam String billSn) {
        return itemService.addItem(manualId, billSn);
    }

    @ApiOperation("编辑时删除一条明细")
    @PostMapping("/deleteItem")
    public CommonResult deleteItem(@RequestParam String itemId) {
        return itemService.deleteItem(itemId);
    }

//    @ApiOperation("手工开蓝票")
//    @PostMapping("/openBill")
//    public CommonResult openBill(Principal principal, @RequestBody List<String> ids) {
//        if (principal == null) return CommonResult.unauthorized(null);
//        return manualInvoiceService.openBill(principal, ids);
//    }
}

